package
{
	import flash.ui.Mouse;
	import org.flixel.*;
	
 
	public class PlayState extends FlxState
	{	
		[Embed(source = "/../media/water.png")] private var ImgWater:Class;
		[Embed(source = "/../media/Dot.png")] private var ImgDot:Class;
		
		[Embed(source="../media/mapCSV_Theden_Theden.csv", mimeType="application/octet-stream")] public var CSV_Theden:Class;
		[Embed(source="../media/Between.png")] public var Img_Theden:Class;
		
		
		public var level1:FlxTilemap;
		private var _player:Player;
		private var npc1:NPC;
		private var npc2:NPC;
	//	private var _projectile:Projectile;
		public var playerMessage:FlxText;
		public var gameMessage:FlxText;
		public var npcGroup:FlxGroup;
	//	public var projectileGroup:FlxGroup;
		public var waterDispenser:FlxEmitter;
		public var textBox:FlxSprite;		
		public var alertBox:FlxSprite;
		
		public var triggerGroup:FlxGroup;
		public var testTrigger:Trigger;
		
		public var chatCount:Number;
		
		override public function create():void
		{
			level1 = new FlxTilemap();
			//level1.setTileProperties(2, FlxObject.FLOOR);
			add(level1.loadMap(new CSV_Theden, Img_Theden, 24, 24));
			
			
			waterDispenser = new FlxEmitter();
			waterDispenser.setSize(1,1);
		//	waterDispenser.setXSpeed(30,50);
		//	waterDispenser.setYSpeed(-50,50);
			waterDispenser.gravity = 300;
			waterDispenser.bounce = 0.1;
			waterDispenser.makeParticles(ImgWater, 1000, 16, false, 0.8);
			//waterDispenser.makeParticles(ImgDot, 1, 16, false, 0.7);
			//waterDispenser.start(false,10,0.035);
			add(waterDispenser);
			
			//instantiation of NPCs
			npcGroup = new FlxGroup();
			npc1 = new NPC(200, 168, 1);
			npc1.addMessage(Global.gameTextData[0]);
			//npc1.addMessage("I am saying something important about the plot.");
			//npc1.addMessage("What I am saying is so significant that you must read every word of it.");
			//npc1.addMessage("I'm also going to talk for a very long time; so long that this text may even overlap our sprites.");
			//npc1.addMessage("If that happens then it means the end of the world, so I should probably stop talking, but there's more that I must tell you so that you can keep the balance between the rifts.");
			//npc1.addMessage("Whatever you do, don't eat the salad.");
			npcGroup.add(npc1);
			npc2 = new NPC(250, 168, 1);
			npc2.addMessage(Global.gameTextData[1]);
			npcGroup.add(npc2);
			add(npcGroup);
			
			//instantiation of the Balance Keeper
			_player = new Player(50, 0);
			add(_player);
			
			
		//	projectileGroup = new FlxGroup();
			
			//"textboxes"
			textBox = new FlxSprite(0,0);
			textBox.makeGraphic(360, 68, 0x44000000);
			add(textBox);
			
			alertBox = new FlxSprite(0,0);
			alertBox.makeGraphic(10, 12, 0x44000000);
			add(alertBox);
			
			playerMessage = new FlxText(100, 100, 10, " ", false);
			playerMessage.alignment = "center";
			add(playerMessage);
			
			gameMessage = new FlxText(5, 10, 360, " ", false);
			//gameMessage.color = 0x000000;
			add(gameMessage);
			
			//var chatCount:Number = 0;
			//add(chatCount);
			FlxG.bgColor = 0xffffffff;
			
			triggerGroup = new FlxGroup();
			testTrigger = new Trigger(240, 456, 24, 24);
			testTrigger.addCommand("cameraAt", 200, 200);
			
			triggerGroup.add(testTrigger);
			add(triggerGroup);
			
			FlxG.camera.follow(_player);
			FlxG.camera.zoom = 2;

 
		}
		override public function update():void
		{
			FlxG.worldBounds = new FlxRect(_player.getMidpoint().x - 200, _player.getMidpoint().y - 200, 400, 400);
			Mouse.show();
			/*REMOVE THIS LATER. I'M JUST SICK OF* 
			 *HAVING TO RESTART THE PROGRAM EVERY* 
			 *TIME I FALL OFF THE MAP*/
			if (_player.y >= 1000)
				_player.y = 0;
			if (_player.x > 870)
				_player.x = -150;
			if (_player.x < -150)
				_player.x = 870;
			/*******************************/
			
						// controlling message boxes
			textBox.visible = false;
			if (gameMessage.text != " ") textBox.visible = true;
			alertBox.visible = false;
			if (playerMessage.text != " ") alertBox.visible = true;
			
			//Global.cameraObject.x = _player.getMidpoint().x;
			//Global.cameraObject.y = _player.getMidpoint().y;
			//FlxG.camera.update();
			
			playerMessage.text = " ";
			playerMessage.x = _player.x+2;
			playerMessage.y = _player.getMidpoint().y - 30;
			gameMessage.x = _player.x - 170;
			gameMessage.y = _player.y - 140;
			textBox.x = _player.x - 170;
			textBox.y = _player.y - 140;
			alertBox.x = _player.x+4;
			alertBox.y = _player.getMidpoint().y - 26;
			
			
			
			
			// dispenser for projectiles
			waterDispenser.at(_player);
			
			
			
			
			//firing mechanism
			if (FlxG.mouse.pressed()) {
				
				// WARNING EXPIREMENTAL SHIZZLE AND STUFF
				var d:Number = Math.sqrt(Math.pow(FlxG.mouse.x - _player.getMidpoint().x + 180, 2) + Math.pow(FlxG.mouse.y - _player.getMidpoint().y + 150, 2));
				var v:Number = Math.min(d * 4, 300); //initial velocity
				var a:Number = ((v * (FlxG.mouse.x - _player.getMidpoint().x + 180)) / d) + _player.velocity.x/2;
				var b:Number = ((v * (FlxG.mouse.y - _player.getMidpoint().y + 150)) / d) + _player.velocity.y/2;
				
				waterDispenser.setXSpeed(a-30,a);
				waterDispenser.setYSpeed(b-30,b);
				waterDispenser.emitParticle();
				//OMFGOMFOMGOMFOMGOMFOMGOMFOMGOMFOM
				
				/*
				_projectile = new Projectile(_player.getMidpoint().x, _player.getMidpoint().y)
				projectileGroup.add(_projectile);
				add(projectileGroup);
				_projectile.fire(FlxG.mouse.getWorldPosition(),_player.velocity);*/
			}
			
			if (!FlxG.overlap(_player, npcGroup)) {
				gameMessage.text = " ";
			//	gameMessage.text = ((int)(cursor.x)).toString() + ", " + ((int)(cursor.y)).toString();
				chatCount = 0;
				
			}
			
			
			// Npc interaction
			FlxG.overlap(_player, npcGroup, onNpcPlayerOverlap);
			FlxG.overlap(_player, triggerGroup, onTriggerPlayerOverlap);
			//
			
			if (FlxG.keys.ESCAPE) {
				FlxG.switchState(new PauseMenuState());
			}
			
			super.update();
			FlxG.collide(_player, level1);
			FlxG.collide(waterDispenser, level1);
			//gameMessage.text = chatCount.toString();
			
						
		}
		
		public function onNpcPlayerOverlap(character:FlxObject,npc:NPC):void {
			if (chatCount != npc.getNumMessages()) {
				playerMessage.text = "F";
				
				if (FlxG.keys.justPressed("F")) {
					gameMessage.text = npc.getMessage(chatCount);
					++chatCount;
				}
				FlxG.keys.update();
			}
		}
		
		public function onTriggerPlayerOverlap(character:FlxObject, trigger:Trigger):void {
			trigger.execute();
			
			trigger.kill();
		}
		
	}
}